#include "ResultModel.h"
#include "AssetModel.h"

namespace Xps
{
	ResultModel::ResultModel(XpsAssets::AssetManager* am) : QAbstractItemModel()
	{
		this->am = am;
		rowcount = 0;
		colcount = 0;
	}

	QVariant ResultModel::data(const QModelIndex & index, int role ) const
	{
		static QIcon ico_range(":/rs/images/range.png");
		static QIcon ico_fade(":/rs/images/fade.png");
		static QIcon ico_exact(":/rs/images/exact.png");

		if (role == Qt::DecorationRole && index.isValid())
		{
			XpsAssets::Result* r = am->getResult(rowids.at(index.row()));
			XpsAssets::ResultAttribute* ra = r->attributes()->value(colids.at(index.column()));

			if (ra->matchMode() == XpsAssets::MatchMode::RANGE)
				return ico_range;
			else if (ra->matchMode() == XpsAssets::MatchMode::FADE)
				return ico_fade;
			else if (ra->matchMode() == XpsAssets::MatchMode::EXACT)
				return ico_exact;
		}
		if (role == Role::VALUE && index.isValid())
		{
			XpsAssets::Result* r = am->getResult(rowids.at(index.row()));
			int val = r->attributes()->value(colids.at(index.column()))->value();
			return val;
		}
		if (role == Role::TOLERANCE && index.isValid())
		{
			XpsAssets::Result* r = am->getResult(rowids.at(index.row()));
			int val = r->attributes()->value(colids.at(index.column()))->tolerance();
			return val;
		}
		if (role == Role::MATCHMODE && index.isValid())
		{
			XpsAssets::Result* r = am->getResult(rowids.at(index.row()));
			int val = r->attributes()->value(colids.at(index.column()))->matchMode();
			return val;
		}
		if (role == Qt::DisplayRole && index.isValid())
		{
			XpsAssets::Result* r = am->getResult(rowids.at(index.row()));
			int val = r->attributes()->value(colids.at(index.column()))->value();
			int tol = r->attributes()->value(colids.at(index.column()))->tolerance();
			
			if (tol != 0)
				return QString("%1 (%2)").arg(val).arg(tol);

			return QString("%1").arg(val);
			//return QString("%1 %2").arg(index.row()).arg(index.column());//root123->child(index.row())->data(role);
		}
		if (role == Qt::EditRole && index.isValid())
		{
			XpsAssets::Result* r = am->getResult(rowids.at(index.row()));
			int val = r->attributes()->value(colids.at(index.column()))->value();
			return QString("%1").arg(val);
		}
		return QVariant();
	}

	bool ResultModel::setData(const QModelIndex & index, const QVariant & value, int role)
	{
		if (role == Qt::EditRole && index.isValid())
		{
			XpsAssets::Result* r = am->getResult(rowids.at(index.row()));
			XpsAssets::ResultAttribute* ra = r->attributes()->value(colids.at(index.column()));
			ra->setValue(value.toInt());
			emit dataChanged(index, index);
			
			return true;
			//return QString("%1 %2").arg(index.row()).arg(index.column());//root123->child(index.row())->data(role);
		}
		if (role == Role::MATCHMODE && index.isValid())
		{
			XpsAssets::Result* r = am->getResult(rowids.at(index.row()));
			XpsAssets::ResultAttribute* ra = r->attributes()->value(colids.at(index.column()));
			ra->setMatchMode(value.toInt());
			emit dataChanged(index, index);
			
			return true;
		}
		if (role == Role::VALUE && index.isValid())
		{
			XpsAssets::Result* r = am->getResult(rowids.at(index.row()));
			XpsAssets::ResultAttribute* ra = r->attributes()->value(colids.at(index.column()));
			ra->setValue(value.toInt());
			emit dataChanged(index, index);
			
			return true;
		}
		if (role == Role::TOLERANCE && index.isValid())
		{
			XpsAssets::Result* r = am->getResult(rowids.at(index.row()));
			XpsAssets::ResultAttribute* ra = r->attributes()->value(colids.at(index.column()));
			ra->setTolerance(value.toInt());
			emit dataChanged(index, index);
			
			return true;
		}
		return false;
	}

	void ResultModel::addResult(int id)
	{
		beginInsertRows(QModelIndex(), 0, 0);
		rowids.append(id);
		endInsertRows();
		rowcount++;
	}


	void ResultModel::remResult(int id)
	{
		int pos = rowids.indexOf(id);
		
		beginRemoveRows(QModelIndex(), pos, pos);
		rowids.removeAt(pos);
		rowcount--;
		endRemoveRows();
	}


	void ResultModel::addAttribute(int id)
	{
		beginInsertColumns(QModelIndex(), 0, 0);
		colids.append(id);
		endInsertColumns();
		colcount++;
	}

	void ResultModel::remAttribute(int id)
	{
		int pos = colids.indexOf(id);
		
		beginRemoveColumns(QModelIndex(), pos, pos);
		colids.removeAt(pos);
		colcount--;
		endRemoveColumns();
	}

	Qt::ItemFlags ResultModel::flags(const QModelIndex & index) const  
	{
		return Qt::ItemFlag::ItemIsSelectable | Qt::ItemFlag::ItemIsEnabled | Qt::ItemFlag::ItemIsEditable;
	}

	int ResultModel::rowCount(const QModelIndex &parent) const
	{
		return rowcount;
	}


	int ResultModel::columnCount(const QModelIndex &parent) const
	{
		return colcount;
	}

	QModelIndex ResultModel::parent(const QModelIndex & index) const
	{
		return QModelIndex();
	}

	QModelIndex ResultModel::index(int row, int column, const QModelIndex &parent) const
	{
		if (!hasIndex(row, column, parent))
			return QModelIndex();

		return createIndex(row, column);
	}

	void ResultModel::clear()
	{
		colcount = 0;
		rowcount = 0;
		colids.clear();
		rowids.clear();
		reset();
	}

	QVariant ResultModel::headerData(int section, Qt::Orientation orientation, int role) const
	{
		static QIcon ico_rslt(":/rs/images/result.png");
		static QIcon ico_attribute(":/rs/images/attribute.png");
		if (orientation == Qt::Orientation::Vertical)
		{
			if (role == Qt::DisplayRole)
				return am->getResult(rowids.at(section))->name();
			if (role == Qt::DecorationRole)
				return ico_rslt;
		}
		if (orientation == Qt::Orientation::Horizontal)
		{
			if (role == Qt::DisplayRole)
			{
				XpsAssets::Attribute* a = am->getAttribute(colids.at(section));
				
				if (a != NULL)
					return a->name();
				
				return "";
			}
			if (role == Qt::DecorationRole)
				return ico_attribute;
			if (role == Qt::TextAlignmentRole)
				return Qt::AlignLeft;
		}
		return QVariant();
	}
}